# 作者: YYJ
# 2025年06月25日21时38分59秒
# 2486249622@qq.com


def bsearch(arr,  target):
    low = 0
    high = len(arr) - 1
    while low <= high:
        mid = (low + high) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            low = mid + 1
        else:
            high = mid - 1
    return -1


class Hash:
    def __init__(self, size):
        self.hash_table = [None] * size
        self.size = size

    def elf_hash(self, hash_str):
        h = 0
        g = 0
        for i in hash_str:
            h = (h << 4) + ord(i)
            g = h & 0xf0000000
            if g:
                h ^= g >> 24
            h &= ~g
        return h % self.size

if __name__ == '__main__':
    arr = [7, 43, 11, 31, 15, 27, 19, 39, 35, 23]
    pos = bsearch(arr, 27)
    print(f"The position of 27 in the array is {pos}") if pos != -1 else print("Not found")
    str_list = ["hello", "world", "python", "hello", "world"]
    h = Hash(1000)
    for i in str_list:
        index = h.elf_hash(i)
        if h.hash_table[index] is None:
            h.hash_table[index] = [i]   # 存储时候，将数据存储在列表中
            print(f"{i} is added to the hash table")
        else:
            print(f"{i} is already in the hash table")
            h.hash_table[index].append(i)   # 如果已经存在，则将数据添加到列表中

    stu_name = input("请输入学生姓名：")
    index = h.elf_hash(stu_name)
    for i in h.hash_table[index]:
        if i == stu_name:
            print(f"{stu_name} is found in the hash table")
            break
    else:
        print(f"{stu_name} is not found in the hash table")


